package com.ecp.base.jcrontab;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.jcrontab.Crontab;
import org.jcrontab.log.Log;

public class ServletJcrontab extends HttpServlet {
	
	private static final long	serialVersionUID	= 1L;
    private Crontab crontab = null;
    public void init() throws ServletException {
        process();
    }

    protected InputStream createPropertiesStream(String name) throws
            IOException {
        return new FileInputStream(name);
    }

    public void process() {

        String propz = "/WEB-INF/jcrontab.properties";
        
        String props = getServletConfig()
                       .getInitParameter("PROPERTIES_FILE");
        if (props == null) {
            props = propz;
        }
        props = this.getServletContext().getRealPath(props);

        Properties propObj = new Properties();
        try {
            InputStream input = createPropertiesStream(props);
            propObj.load(input);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        ServletConfig c = getServletConfig();
        Enumeration keys = c.getInitParameterNames();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            propObj.setProperty(key, c.getInitParameter(key));
        }
        String log4jpath = propObj.getProperty(
                "org.jcrontab.log.log4J.Properties");
        if (log4jpath != null) {
            log4jpath = this.getServletContext().getRealPath(log4jpath);
            propObj.setProperty("org.jcrontab.log.log4J.Properties", log4jpath);
        }

        String jcrontabconfig = propObj.getProperty("org.jcrontab.config");
        if (jcrontabconfig != null) {
            jcrontabconfig = this.getServletContext().getRealPath(
                    jcrontabconfig);
            propObj.setProperty("org.jcrontab.config", jcrontabconfig);
        }

        String datafile = propObj.getProperty("org.jcrontab.data.file");
        String dd;
        if (datafile != null) {
            datafile = this.getServletContext().getRealPath(datafile);
            propObj.setProperty("org.jcrontab.data.file", datafile);
            dd= propObj.getProperty("org.jcrontab.data.file");
        }

        crontab = Crontab.getInstance();

        try {
            ShutdownHook();
            crontab.init(propObj);

        } catch (Exception e) {
            Log.error(e.toString(), e);
        }
    }

    /**
     * This method seths a ShutdownHook to the system
     *  This traps the CTRL+C or kill signal and shutdows
     * Correctly the system.
     * @throws Exception
     */
    public void ShutdownHook() throws Exception {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                doStop();
            }
        });
    }

    public void destroy() {
        doStop();
    }

    public void doStop() {
        Log.info("Shutting down...");
        crontab.uninit(100);
        Log.info("Stoped");
    }

}
